<template>
	<view class="page" :style="{ 'height': h + 'px', 'padding-top': mt + 'px' }">
		<c-nav-bar title="选择订单"></c-nav-bar>
		<view class="tabs">
			<view v-for="(item, index) in tabList" :key="index" :class="tidx == index ? 'active' : ''"
				@tap="changeTab(index)">
				{{ item }}
			</view>
		</view>
		<view class="list" v-if="list.length > 0">
			<u-list @scrolltolower="scrolltolower">
				<u-list-item v-for="(item, index) in list" :key="index">
					<view class="l_cstp" v-if="tidx == 0">
						<view class="top">
							<view class="left">
								<image @tap="selectItem(item, index)" v-if="!item.select"
									src="https://i.ringzle.com/file/20231027/db588133d67548fc82dfb0d128eac9a8.png">
								</image>
								<image @tap="selectItem(item, index)" v-else
									src="https://i.ringzle.com/file/20240318/914f08290ea9411c902c294151d74b71.png">
								</image>
								<text>船宿通票</text>
							</view>
							<view class="right">
								<text>已完成</text>
							</view>
						</view>
						<view class="content">
							<view class="title">{{ item.jointName }}</view>
							<view class="date">
								<text>{{ item.arriveDate }}</text>
								<text>至</text>
								<text>{{ item.leaveDate }}</text>
							</view>
							<view class="room_price">
								<view class="room">
									<text>{{ item.num }}晚{{ item.checkInRooms }}间</text>
									<text>{{ item.houseBaseName }}</text>
								</view>
								<view class="price">
									<text>¥{{ item.realityPay }}</text>
								</view>
							</view>
						</view>
					</view>
					<view class="l_cjtp" v-if="tidx == 1">
						<view class="top">
							<view class="left">
								<image @tap="selectItem(item, index)" v-if="!item.select"
									src="https://i.ringzle.com/file/20231027/db588133d67548fc82dfb0d128eac9a8.png">
								</image>
								<image @tap="selectItem(item, index)" v-else
									src="https://i.ringzle.com/file/20240318/914f08290ea9411c902c294151d74b71.png">
								</image>
								<text>船景通票</text>
							</view>
							<view class="right">
								<text>已完成</text>
							</view>
						</view>
						<view class="content">
							<view class="title">{{ item.jointName }}</view>
							<view class="date">
								<text>游玩日期 {{ item.sailDate || '' }}</text>
								<text>{{ item.priceType == 1 ? '成人票' : '儿童票' }} X{{ item.num }}</text>
							</view>
							<view class="room_price">
								<text>¥{{ item.realityPay || '' }}</text>
							</view>
						</view>
					</view>
					<view class="l_ship" v-if="tidx == 2">
						<view class="top">
							<view class="left">
								<image @tap="selectItem(item, index)" v-if="!item.select"
									src="https://i.ringzle.com/file/20231027/db588133d67548fc82dfb0d128eac9a8.png">
								</image>
								<image @tap="selectItem(item, index)" v-else
									src="https://i.ringzle.com/file/20240318/914f08290ea9411c902c294151d74b71.png">
								</image>
								<text>船票</text>
							</view>
							<view class="right">
								<text>已完成</text>
							</view>
						</view>
						<view class="content">
							<view v-for="(person, idx) in item.detailDTOList" :key="idx">
								<view class="title">
									<text>{{ item.startPortName }} - {{ item.endPortName }} - {{ item.lineNum+''+item.sx }}</text>
									<text>￥{{ person.realFee }}</text>
								</view>
								<view class="date">
									<text>{{ item.nyr }} {{ item.sf }}出发</text>
								</view>
								<view class="room_price">
									<text>{{ person.passName }}</text>
								</view>
							</view>
						</view>
					</view>
					<view class="l_hotel" v-if="tidx == 3">
						<text class="status s3">已完成</text>
						<view class="head">
							<image @tap="selectItem(item, index)" v-if="!item.select"
								src="https://i.ringzle.com/file/20231027/db588133d67548fc82dfb0d128eac9a8.png"></image>
							<image @tap="selectItem(item, index)" v-else
								src="https://i.ringzle.com/file/20240318/914f08290ea9411c902c294151d74b71.png"></image>
							<text>住宿</text>
						</view>
						<view class="mid">
							<view class="name">{{ item.homestayName }}</view>
							<view class="date">入离日期 {{ item.arriveDate }} 至 {{ item.leaveDate }} </view>
							<view class="des">{{ item.houseBaseName }}</view>
						</view>
						<view class="price">¥{{ item.orderAmount }}</view>
					</view>
					<view class="l_jd" v-if="tidx == 4">
						<view class="top">
							<view class="left">
								<image @tap="selectItem(item, index)" v-if="!item.select"
									src="https://i.ringzle.com/file/20231027/db588133d67548fc82dfb0d128eac9a8.png">
								</image>
								<image @tap="selectItem(item, index)" v-else
									src="https://i.ringzle.com/file/20240318/914f08290ea9411c902c294151d74b71.png">
								</image>
								<text>景点门票</text>
							</view>
							<view class="right">
								<text>已完成</text>
							</view>
						</view>
						<view class="content">
							<view class="title">{{ item.scenicSpotName }}</view>
							<view class="date">
								<text>游玩时间</text>
								<text>{{ item.playDate }}</text>
							</view>
							<view class="room_price">
								<view class="room">
									<text>游客 X1</text>
								</view>
								<view class="price">
									<text>¥{{ item.realityPay }}</text>
								</view>
							</view>
						</view>
					</view>
					<view class="l_park" v-if="tidx == 5">
						<view class="i_left">
							<image @tap="selectItem(item, index)" v-if="!item.select"
								src="https://i.ringzle.com/file/20231027/db588133d67548fc82dfb0d128eac9a8.png"></image>
							<image @tap="selectItem(item, index)" v-else
								src="https://i.ringzle.com/file/20240318/914f08290ea9411c902c294151d74b71.png"></image>
							<view class="il_info">
								<view>{{ item.nameIsland }}-{{ item.parkadeName }}</view>
								<view class="ili_licence">{{ item.numberplate }}</view>
								<view class="ili_date">{{ item.paymentTime }}</view>
							</view>
						</view>
						<view class="i_right">-{{ item.paymentAmount.toFixed(2) }}</view>
					</view>
				</u-list-item>
			</u-list>
		</view>
		<view class="empty" v-else>
			<text>您还没有可开票订单</text>
		</view>
		<view class="bottom">
			<view class="b_box">
				<view class="b_left">
					<view class="bl_qx">
						<image @tap="selectAll" v-if="!isall"
							src="https://i.ringzle.com/file/20231027/db588133d67548fc82dfb0d128eac9a8.png"></image>
						<image @tap="selectAll" v-else
							src="https://i.ringzle.com/file/20240318/914f08290ea9411c902c294151d74b71.png"></image>
						<text>全选</text>
					</view>
					<view class="bl_text">
						<text>已选{{ orderNum }}个订单</text>
						<!-- <text>共￥{{price}}</text> -->
						<text>共￥{{ Math.round(price * 100) / 100 }}</text>
					</view>
				</view>
				<view class="b_right" @tap="toNext">下一步</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				h: uni.getSystemInfoSync().windowHeight - 82,
				mt: uni.getSystemInfoSync().statusBarHeight + 44,
				tidx: 0,
				tabList: ['船宿通票', '船景通票', '出行', '住宿', '景点', '停车'],
				isall: false,
				orderNum: 0,
				price: 0,
				list: [],
				pageIndex: 1,
				pageCount: 10,
				isOver: false,
				orderState: '',
				urls: {
					0: '/api/shipHotel/getShPersonalOrderList',
					1: '/api/shipScenic/getSsPersonalOrderList',
					2: '/api/travel/isshiporder/page',
					3: '/api/hotel/getCenterOrderPage',
					4: '/api/scenic/selectOrderPage',
					5: '/travel/wisdom/invoicing/invoicing'
				},
			}
		},
		onLoad(option) {
			if (option.type) this.tidx = +option.type;
			this.getList();
		},
		methods: {
			getList() {
				let params = {
					openId: JSON.parse(uni.getStorageSync('userInfo')).openId,
					orderState: '',
					page: this.pageIndex,
					limit: this.pageCount
				}

				if (this.tidx == 2) params.orderType = '3,9';
				if (this.tidx == 3 || this.tidx == 4) {
					delete params.orderState;
					params.status = '';
				}
				if (this.tidx == 5) {
					delete params.orderState;
				}

				this.$api.get(this.urls[this.tidx], params).then(res => {
					// console.log(res.data)
					if (res.data.code === 0) {
						this.pageIndex++;
						let list = res.data.data.list;
						let total = res.data.data.total;
						if (this.tidx == 4) list = res.data.data.ticketOrderList;
						if (this.tidx == 5) list = res.data.data;

						if (this.list.length + list.length > total || list.length == 0) return this.isOver = true;
						this.list = [...this.list, ...list];
						// console.log('this.list', this.list);
						this.list.forEach((l, i) => {
							if (!this.list[i].select) this.$set(this.list[i], 'select', false)
						});
						if (this.tidx == 0) {
							this.list.forEach((d, i) => {
								d.leaveDate = new Date(d.leaveDate).Format('yyyy-MM-dd');
								d.arriveDate = new Date(d.arriveDate).Format('yyyy-MM-dd');
							})
						}
						if (this.tidx == 2) {
							this.list.forEach((d, i) => {
								d.nyr = new Date(d.sailDate).Format('yyyy年MM月dd日');
								d.sf = new Date(new Date().Format('yyyy-MM-dd') + ' ' + d.sailTime).Format(
									'hh:mm');
							})
						}

					} else this.$showModal(res.data.msg);
				})
			},
			changeTab(index) {
				this.tidx = index;
				this.initList();
			},
			initList() {
				this.pageIndex = 1;
				this.isOver = false;
				this.list = [];
				this.orderNum = 0;
				this.price = 0;
				this.isall = false;
				this.getList();
			},
			selectItem(item, index) {
				this.$set(this.list[index], 'select', !this.list[index].select);
				let snum = this.list.filter(l => l.select == true).length;
				if (snum == this.list.length) this.isall = true
				else this.isall = false;
				this.dealNumAndPrice();
			},
			selectAll() {
				this.isall = !this.isall;
				this.list.forEach((l, i) => this.$set(this.list[i], 'select', this.isall));
				this.dealNumAndPrice();
			},
			dealNumAndPrice() {
				let slist = this.list.filter(l => l.select == true);
				this.orderNum = slist.length;
				if ([0, 1, 4].includes(this.tidx)) {
					this.price = slist.reduce((cur, pre) => cur + pre.realityPay, 0);
				} else if (this.tidx == 2) {
					this.price = slist.reduce((cur, pre) => cur + pre.totalPayFee, 0);
				} else if (this.tidx == 3) {
					this.price = slist.reduce((cur, pre) => cur + pre.orderAmount, 0);
				} else if (this.tidx == 5) {
					this.price = slist.reduce((cur, pre) => cur + pre.paymentAmount, 0);
				}
			},
			toNext() {
				if (!this.orderNum) return this.$showToast('请至少选择一条订单');
				uni.navigateTo({
					url: '/pagesMy/invoice/invoice?price=' + this.price
				})
			},
			scrolltolower() {
				if (this.isOver) return
				this.getList();
			}
		}
	}
</script>

<style scoped lang="less">
	.page {
		background: #F5F8FA;
		box-sizing: border-box;
		overflow-y: auto;

		.tabs {
			width: 100%;
			height: 102rpx;
			display: flex;
			background: #fff;

			&>view {
				width: 12.5%;
				height: 102rpx;
				line-height: 102rpx;
				text-align: center;
				position: relative;
				font-size: 28rpx;
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				color: #111111;

				&:nth-child(1),
				&:nth-child(2) {
					width: calc(25%);
				}

				&.active {
					font-size: 30rpx;
					font-family: PingFang-SC, PingFang-SC;
					font-weight: bold;
					color: #007A69;

					&::after {
						content: "";
						width: 64rpx;
						height: 6rpx;
						background: #007A69;
						border-radius: 2rpx;
						position: absolute;
						left: 50%;
						margin-left: -32rpx;
						bottom: 7rpx;
					}
				}
			}
		}

		.list {
			width: 100%;
			height: calc(100% - 102rpx);
			overflow-y: auto;

			::v-deep .u-list {
				width: 100%;
				height: 100% !important;
				padding: 0 24rpx 20rpx;
				box-sizing: border-box;
			}

			.l_cstp {
				width: 100%;
				margin-top: 20rpx;
				border-radius: 16rpx;
				background: #fff;
				padding: 40rpx 24rpx;
				box-sizing: border-box;

				.top {
					display: flex;
					justify-content: space-between;

					.left {
						display: flex;
						align-items: center;

						image {
							width: 40rpx;
							height: 40rpx;
						}

						text {
							font-size: 28rpx;
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							color: #333333;
							line-height: 28rpx;
							margin-left: 20rpx;
						}
					}

					.right {
						text-align: right;
						font-size: 26rpx;
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						color: #333333;
					}
				}

				.content {
					padding: 28rpx 0 0 56rpx;
					background: #fff;

					.title {
						font-size: 32rpx;
						font-family: PingFang-SC-Bold, PingFang-SC;
						font-weight: bold;
						color: #333333;
						line-height: 45rpx;
					}

					.date {
						display: flex;
						align-items: center;
						margin-top: 31rpx;

						&>text {
							font-size: 26rpx;
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							color: #808080;
							line-height: 24rpx;

							&:nth-child(2) {
								margin: 0 10rpx;
							}
						}
					}

					.room_price {
						display: flex;
						justify-content: space-between;
						margin-top: 30rpx;

						.room {
							display: flex;
							align-items: center;

							&>text {
								font-size: 26rpx;
								font-family: PingFangSC, PingFang SC;
								font-weight: 400;
								color: #808080;

								&:last-child {
									margin-left: 30rpx;
								}
							}
						}

						.price {
							text {
								font-size: 32rpx;
								font-family: PingFang-SC, PingFang-SC;
								font-weight: bold;
								color: #333333;
							}
						}
					}
				}
			}

			.l_cjtp {
				width: 100%;
				margin-top: 20rpx;
				border-radius: 16rpx;
				background: #fff;
				padding: 40rpx 24rpx;
				box-sizing: border-box;

				.top {
					display: flex;
					justify-content: space-between;

					.left {
						display: flex;
						align-items: center;

						image {
							width: 40rpx;
							height: 40rpx;
						}

						text {
							font-size: 28rpx;
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							color: #333333;
							line-height: 28rpx;
							margin-left: 20rpx;
						}
					}

					.right {
						text-align: right;
						font-size: 26rpx;
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						color: #333333;
					}
				}

				.content {
					padding: 28rpx 0 0 56rpx;
					position: relative;

					.title {
						font-size: 32rpx;
						font-family: PingFang-SC, PingFang-SC;
						font-weight: bold;
						color: #333333;
						line-height: 32rpx;
					}

					.date {
						display: flex;
						flex-direction: column;

						text {
							font-size: 26rpx;
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							color: #808080;
							line-height: 26rpx;
							margin-top: 31rpx;
						}
					}

					.room_price {
						position: absolute;
						bottom: 0;
						right: 0;
						z-index: 1;

						text {
							font-size: 32rpx;
							font-family: PingFang-SC, PingFang-SC;
							font-weight: bold;
							color: #333333;
							line-height: 32rpx;
						}
					}
				}
			}

			.l_ship {
				width: 100%;
				margin-top: 20rpx;
				border-radius: 16rpx;
				background: #fff;
				padding: 40rpx 24rpx;
				box-sizing: border-box;

				.top {
					display: flex;
					justify-content: space-between;

					.left {
						display: flex;
						align-items: center;

						image {
							width: 40rpx;
							height: 40rpx;
						}

						text {
							font-size: 28rpx;
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							color: #333333;
							margin-left: 20rpx;
						}
					}

					.right {
						text-align: right;
						font-size: 26rpx;
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						color: #333333;
					}
				}

				.content {
					padding: 28rpx 0 0 56rpx;
					background: #fff;

					&>view {
						padding-bottom: 32rpx;
						margin-bottom: 32rpx;
						border-bottom: 1rpx solid #EFEFEF;

						&:last-child {
							padding-bottom: 0;
							margin-bottom: 0;
							border-bottom: none
						}

						.title {
							display: flex;
							align-items: center;
							justify-content: space-between;

							text {
								font-size: 32rpx;
								font-family: PingFang-SC-Bold, PingFang-SC;
								font-weight: bold;
								color: #333333;
							}
						}

						.date {
							margin-top: 31rpx;

							text {
								font-size: 26rpx;
								font-family: PingFangSC, PingFang SC;
								font-weight: 400;
								color: #808080;
								line-height: 26rpx;
							}
						}

						.room_price {
							margin-top: 30rpx;
							font-size: 26rpx;
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							color: #808080;
						}
					}
				}
			}

			.l_hotel {
				width: 100%;
				margin-top: 20rpx;
				border-radius: 16rpx;
				background: #fff;
				padding: 40rpx 24rpx;
				box-sizing: border-box;
				position: relative;

				.status {
					right: 24rpx;
					top: 40rpx;
					font-size: 26rpx;
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					color: #333333;
					position: absolute;
				}

				.head {
					color: #111;

					image {
						width: 40rpx;
						height: 40rpx;
						margin-right: 20rpx;
						vertical-align: middle;
					}

					text {
						font-size: 28rpx;
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						color: #333333;
						vertical-align: middle;
					}
				}

				.mid {
					padding: 28rpx 50rpx 0 56rpx;

					.name {
						font-size: 32rpx;
						font-family: PingFang-SC, PingFang-SC;
						font-weight: bold;
						color: #333333;
					}

					.date {
						margin-top: 32rpx;
						font-size: 24rpx;
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						color: #808080;
					}

					.des {
						margin-top: 32rpx;
						font-size: 24rpx;
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						color: #808080;
					}
				}

				.price {
					position: absolute;
					right: 24rpx;
					bottom: 40rpx;
					font-size: 32rpx;
					font-family: PingFang-SC, PingFang-SC;
					font-weight: bold;
					color: #333333;
				}
			}

			.l_jd {
				width: 100%;
				margin-top: 20rpx;
				border-radius: 16rpx;
				background: #fff;
				padding: 40rpx 24rpx;
				box-sizing: border-box;

				.top {
					display: flex;
					justify-content: space-between;

					.left {
						display: flex;
						align-items: center;

						image {
							width: 40rpx;
							height: 40rpx;
						}

						text {
							font-size: 28rpx;
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							color: #333333;
							margin-left: 20rpx;
						}
					}

					.right {
						text-align: right;
						font-size: 26rpx;
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						color: #333333;
					}
				}

				.content {
					padding: 28rpx 0 0 56rpx;
					background: #fff;

					.title {
						font-size: 32rpx;
						font-family: PingFang-SC, PingFang-SC;
						font-weight: bold;
						color: #333333;
						line-height: 45rpx;
					}

					.date {
						display: flex;
						align-items: center;
						margin-top: 32rpx;

						&>text {
							font-size: 24rpx;
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							color: #808080;

							&:nth-child(1) {
								margin-right: 20rpx;
								letter-spacing: 2rpx;
							}
						}
					}

					.room_price {
						display: flex;
						justify-content: space-between;
						margin-top: 32rpx;

						.room {
							display: flex;
							align-items: center;

							&>text {
								font-size: 24rpx;
								font-family: PingFangSC, PingFang SC;
								font-weight: 400;
								color: #808080;
								line-height: 24rpx;
							}
						}

						.price {
							text {
								font-size: 32rpx;
								font-family: PingFang-SC, PingFang-SC;
								font-weight: bold;
								color: #333333;
							}
						}
					}
				}
			}

			.l_park {
				width: 100%;
				background: #FFFFFF;
				border-radius: 20rpx;
				margin-top: 20rpx;
				padding: 26rpx 24rpx 20rpx 30rpx;
				box-sizing: border-box;
				display: flex;
				justify-content: space-between;

				.i_left {
					display: flex;
					align-items: center;

					image {
						width: 34rpx;
						height: 34rpx;
					}

					.il_info {
						margin-left: 33rpx;

						&>view {
							font-size: 32rpx;
							font-family: PingFang SC;
							font-weight: 400;
							color: #333333;

							&.ili_licence {
								margin-top: 4rpx;
							}

							&.ili_date {
								font-size: 28rpx;
								color: #999999;
								margin-top: 20rpx;
							}
						}
					}
				}

				.i_right {
					font-size: 32rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #FE401A;
				}
			}
		}

		.empty {
			width: 100%;
			height: calc(100% - 102rpx);
			display: flex;
			align-items: center;
			justify-content: center;

			text {
				font-size: 30rpx;
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				color: #808080;
			}
		}

		.bottom {
			width: 100%;
			height: 164rpx;
			background: #fff;
			position: fixed;
			bottom: 0;
			left: 0;
			z-index: 2;

			.b_box {
				width: 100%;
				padding: 21rpx 30rpx 0;
				box-sizing: border-box;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.b_left {
					display: flex;
					align-items: center;

					.bl_qx {
						display: flex;
						align-items: center;

						image {
							width: 40rpx;
							height: 40rpx;
						}

						text {
							font-size: 26rpx;
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							color: #808080;
							margin-left: 20rpx;
						}
					}

					.bl_text {
						margin-left: 24rpx;
						display: flex;
						flex-direction: column;

						text {
							font-size: 28rpx;
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							color: #333333;

							&:last-child {
								margin-top: 12rpx;
							}
						}
					}
				}

				.b_right {
					width: 218rpx;
					height: 80rpx;
					background: #007A69;
					border-radius: 46rpx;
					border: 1rpx solid #007A69;
					line-height: 80rpx;
					text-align: center;
					font-size: 32rpx;
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					color: #FFFFFF;
					letter-spacing: 2rpx;
				}
			}
		}
	}
</style>